package com.cos.openapi.services.impl;

import com.cos.openapi.common.ResponseResult;
import com.cos.openapi.common.ResultTable;
import com.cos.openapi.dao.MenuMapper;
import com.cos.openapi.entity.Menu;
import com.cos.openapi.services.MenuServices;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author: COS
 * @time: 2021/11/24 9:53
 * @description:
 */
@Service
public class MenuServicesImpl implements MenuServices {

    @Autowired
    private MenuMapper menuMapper;

    @Override
    public Menu getMenuById(Integer id) {
        return menuMapper.selectByPrimaryKey(id);
    }

    @Override
    public ResultTable getList(Integer page, Integer limit) {
//        PageHelper.startPage(page, limit);
        List<Menu> mlist = menuMapper.getList();
//        PageInfo pi = new PageInfo(mlist);

        return new ResultTable(0, "", mlist.size(),mlist);
    }

    @Override
    public List<Menu> getAllMenuTree() {
        List<Menu> all = menuMapper.getAllMenu();
        //1 找到父菜单 一级菜单
        List<Menu> fu = new ArrayList<>();
        for (Menu menu : all) {
            if (menu.getParentId() == null) {
                fu.add(menu);
            }
        }
        //2 遍历父节点
        for (Menu fuMenu : fu) {
            //创建子节点的集合
            List<Menu> ziMenu = new ArrayList<>();
            for (Menu zi : all) {
                if (zi.getParentId() != null && zi.getParentId().equals(fuMenu.getId())) {
                    ziMenu.add(zi);
                }
            }
            //遍历所有菜单节点,找父菜单 一级菜单的子菜单
            //把子菜单集合赋值给父节点
            fuMenu.setChildren(ziMenu);
        }
        return fu;
    }

    @Override
    public List<Menu> getMenuByUid(Integer id) {
        if (id > 0) {
            //获取用户可以访问的所有的菜单
            List<Menu> all = menuMapper.getMenuByUid(id);
            List<Menu> collect = all.stream().filter(x -> x.getParentId() == null).
                    map(x -> findChildren(x, all)).
                    collect(Collectors.toList());
            return collect;
        }
        return null;
    }

    /**
     * @author: COS
     * @return:
     * @time: 2021/11/24 11:02
     * @description: 查找子级菜单的方法
     */
    public Menu findChildren(Menu m, List<Menu> all) {
        List<Menu> collect = all.stream().
                filter(x -> x.getParentId() != null && m.getId().equals(x.getParentId()) && x.getType() != 2).
                map(x -> findChildren(x, all)).
                collect(Collectors.toList());
        m.setChildren(collect);
        return m;
    }


    public List<Menu> getMenuByUid_old(Integer id) {
        if (id > 0) {
            //获取用户可以访问的所有的菜单
            List<Menu> all = menuMapper.getMenuByUid(id);
            //处理菜单设置父级(一级菜单),子级菜单(二级/三级/四级)
            //先找父级菜单(一级菜单)
            //保存父级(一级)菜单的集合
            List<Menu> fu = new ArrayList<>();
            for (Menu menu : all) {
                //父id为Null即为父级菜单(一级菜单)
                if (menu.getParentId() == null) {
                    fu.add(menu);
                }
            }
            //在一个循环
            for (Menu menu : fu) {
                List<Menu> erzis = new ArrayList<>();
                for (Menu zi : all) {
                    if (zi.getParentId() != null && zi.getParentId().equals(menu.getId()) && zi.getType() != 2) {
                        //找到儿子了,子菜单
                        erzis.add(zi);
                    }
                }
                //把子菜单赋值给父对象
                menu.setChildren(erzis);
            }
            return fu;
        }
        return null;
    }
}
